<template>
  <div class="page-wrapper">
    <el-card class="card">
      <!-- 公司信息行 -->
      <TreeTools :data="companyInfo" :is-component="true" @showDialog="showDialog" />
      <!-- 分割线 -->
      <hr>
      <!-- 部门树形结构 -->
      <el-tree :data="treeData" default-expand-all>
        <template v-slot="scope">
          <!-- 部门名称:{{ scope.data.name }} 负责人: {{ scope.data.manager }} -->
          <TreeTools :data="scope.data" @showDialog="showDialog" @showEditDialog="showEditDialog" @reload="loadList" />
        </template>
      </el-tree>
    </el-card>
    <!-- 弹窗 -->
    <!-- <el-button @click="isShowDialog=true"> 打开弹窗</el-button> -->
    <!-- <DeptDialog :is-show-dialog="isShowDialog" :data="data" @close="isShowDialog=false" @reload="loadList" /> -->
    <!--
      sync 修饰符固定写法, 父组件部分, 只要子组件有 update:props字段名
      父组件就可以在这个字段中添加 .sync 修饰符让对应的数据改为子组件传出的值
     -->
    <DeptDialog ref="deptDialog" :is-show-dialog.sync="isShowDialog" :data="data" @reload="loadList" />
  </div>
</template>

<script>
import TreeTools from './components/tree-tools.vue'
import DeptDialog from './components/dept-dialog.vue'
import { getDepts, getDeptDetail } from '@/api/departments'
import { listToTree } from '@/utils/index'

export default {
  components: {
    TreeTools,
    DeptDialog
  },
  data() {
    return {
      companyInfo: {
        name: '传智教育黑马程序员',
        manager: '负责人',
        id: ''
      },
      treeData: [],
      isShowDialog: false,
      data: {}
    }
  },
  created() {
    this.loadList()
  },
  methods: {
    async loadList() {
      const { depts } = await getDepts()
      // console.log(1, depts)
      this.treeData = listToTree(depts, '')
    },
    showDialog(data) {
      console.log(data)
      this.data = data
      this.isShowDialog = true
    },
    async showEditDialog(data) {
      // console.log(data)
      const res = await getDeptDetail(data.id)
      console.log(res)
      this.$refs.deptDialog.formData = res
      this.isShowDialog = true
    }
  }
}
</script>

<style lang="scss" scoped>
.card {
  padding: 30px 140px;
}

.dropdown-btn {
  cursor: pointer;
}

.info {
  width: 100%;
}
</style>
